
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.FodderType;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage FodderType entities.
 * 
 */
@Repository("FodderTypeDAO")

@Transactional
public class FodderTypeDAOImpl extends AbstractJpaDao<FodderType> implements FodderTypeDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			FodderType.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new FodderTypeDAOImpl
	 *
	 */
	public FodderTypeDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findFodderTypeByName
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeByName(String name) throws DataAccessException {

		return findFodderTypeByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeByName", startResult, maxRows, name);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findFodderTypeByPrimaryKey
	 *
	 */
	@Transactional
	public FodderType findFodderTypeByPrimaryKey(Integer id) throws DataAccessException {

		return findFodderTypeByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeByPrimaryKey
	 *
	 */

	@Transactional
	public FodderType findFodderTypeByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFodderTypeByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.FodderType) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findFodderTypeBySupply
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeBySupply(String supply) throws DataAccessException {

		return findFodderTypeBySupply(supply, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeBySupply
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeBySupply(String supply, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeBySupply", startResult, maxRows, supply);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findFodderTypeByMemoContaining
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeByMemoContaining(String memo) throws DataAccessException {

		return findFodderTypeByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllFodderTypes
	 *
	 */
	@Transactional
	public Set<FodderType> findAllFodderTypes() throws DataAccessException {

		return findAllFodderTypes(-1, -1);
	}

	/**
	 * JPQL Query - findAllFodderTypes
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findAllFodderTypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFodderTypes", startResult, maxRows);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findFodderTypeByNameContaining
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeByNameContaining(String name) throws DataAccessException {

		return findFodderTypeByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findFodderTypeById
	 *
	 */
	@Transactional
	public FodderType findFodderTypeById(Integer id) throws DataAccessException {

		return findFodderTypeById(id, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeById
	 *
	 */

	@Transactional
	public FodderType findFodderTypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFodderTypeById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.FodderType) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findFodderTypeBySupplyContaining
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeBySupplyContaining(String supply) throws DataAccessException {

		return findFodderTypeBySupplyContaining(supply, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeBySupplyContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeBySupplyContaining(String supply, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeBySupplyContaining", startResult, maxRows, supply);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * JPQL Query - findFodderTypeByMemo
	 *
	 */
	@Transactional
	public Set<FodderType> findFodderTypeByMemo(String memo) throws DataAccessException {

		return findFodderTypeByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findFodderTypeByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<FodderType> findFodderTypeByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFodderTypeByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<FodderType>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(FodderType entity) {
		return true;
	}
}
